package cn.iocoder.yudao.module.train.service.student;

import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentUserDTO;
import cn.iocoder.yudao.module.train.dal.mysql.WxUser.WxUserMapper;
import cn.iocoder.yudao.module.train.utils.DateUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.student.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.student.StudentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 学生用户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private WxUserMapper wxUserMapper;

    @Override
    public Long createStudent(StudentSaveReqVO createReqVO) {
        // 插入
        StudentDO student = BeanUtils.toBean(createReqVO, StudentDO.class);
        student.setStudentAge(DateUtils.calculateAge(student.getStudentBirthdate()));
        studentMapper.insert(student);

        // 返回
        return student.getId();
    }

    @Override
    public void updateStudent(StudentSaveReqVO updateReqVO) {
        // 校验存在
        validateStudentExists(updateReqVO.getId());
        if (wxUserMapper.selectById(updateReqVO.getUserId()) == null) {
            throw exception(WX_USER_NOT_EXISTS);
        }
        // 更新
        StudentDO updateObj = BeanUtils.toBean(updateReqVO, StudentDO.class);
        updateObj.setStudentAge(DateUtils.calculateAge(updateObj.getStudentBirthdate()));

        WxUserDO wxUpdateObj = BeanUtils.toBean(updateReqVO, WxUserDO.class);
        wxUpdateObj.setId(updateObj.getUserId());
        wxUserMapper.updateById(wxUpdateObj);
        studentMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudent(Long id) {
        // 校验存在
        validateStudentExists(id);
        // 删除
        studentMapper.deleteById(id);
    }

    @Override
        public void deleteStudentListByIds(List<Long> ids) {
        // 删除
        studentMapper.deleteByIds(ids);
        }


    private void validateStudentExists(Long id) {
        if (studentMapper.selectById(id) == null) {
            throw exception(STUDENT_NOT_EXISTS);
        }
    }

    @Override
    public StudentUserDTO getStudent(Long id) {
        return studentMapper.selectStudentUser(id);
    }

    @Override
    public PageResult<StudentUserDTO> getStudentPage(StudentPageReqVO pageReqVO) {
        return studentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudentUserDTO> getStudentList() {
        return studentMapper.selectStudentList();
    }

    @Override
    public StudentUserDTO getStudentByUserId(Long userId) {
        return studentMapper.selectStudentUserByUserId(userId);
    }

    @Override
    public StudentUserDTO getStudentByPhoneNumber(String phoneNumber) {
        return studentMapper.getStudentByPhoneNumber(phoneNumber);
    }
}
